
package com.hshit.downloader.adapter;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListPopupWindow;
import android.widget.TextView;

import com.hshit.downloader.R;
import com.hshit.downloader.models.TrackInfo;


public class ListMp3Adapter extends BaseAdapter {
    List<TrackInfo> mListVideo;

    private Context mContext = null;


    private IOnItemOptionClickListener mListener = null;
    
    private ListPopupWindow mListPopupWindow;

    public interface IOnItemOptionClickListener {
        void onOptionClick(View v, int position, int option);
    }

    /**
     * @param
     */
    public ListMp3Adapter(Context ctx, List<TrackInfo> objects, IOnItemOptionClickListener l) {
        mContext = ctx;
        mListener = l;
        mListVideo = objects;
    }

    @Override
    public int getCount() {
        return mListVideo.size();
    }

    @Override
    public TrackInfo getItem(int position) {
        return mListVideo.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        TrackInfo item = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_adapter_list_video, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.btn_option = (ImageButton) convertView.findViewById(R.id.btn_option);
            viewHolder.mTitle = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.mSinger = (TextView) convertView.findViewById(R.id.tv_singer);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        
		String[] resArray = mContext.getResources().getStringArray(R.array.list_option);
		List<String> resArrayList = Arrays.asList(resArray);
		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.spinner_item, resArrayList);
        viewHolder.btn_option.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mListener != null) {
                    
                    mListPopupWindow = new ListPopupWindow(mContext);
                    mListPopupWindow.setAdapter(adapter);
                    mListPopupWindow.setModal(true);
                    mListPopupWindow.setContentWidth(250);
                    mListPopupWindow.setAnchorView(viewHolder.btn_option);
                    mListPopupWindow.setOnItemClickListener(new OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int option, long arg3) {
							mListener.onOptionClick(v, position, option);
							mListPopupWindow.dismiss();
						}
					});
                    mListPopupWindow.show();
                }
            }
        });
        
        viewHolder.mTitle.setText(item.getTrack_name());
		viewHolder.mSinger.setText(item.getSinger());
		

        return convertView;
    }

    private class ViewHolder {
        public ImageButton btn_option;
        public TextView mSinger;
        public TextView mTitle;
    }

    public Context getContext() {
        return mContext;
    }
}
